package com.guodefu.demo.designpattern.strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class StrategyContext {
    private final Map<String, Strategy> strategyMap;

    /**
     * 这是 Spring 框架中一种非常强大的功能，称为 类型安全的集合装配（Type-Safe Collections Autowiring）
     * 可以自动地收集某种类型的所有 Bean，并将它们注入到一个集合中
     * @param strategyMap 策略集合
     */
    @Autowired
    public StrategyContext(Map<String, Strategy> strategyMap) {
        this.strategyMap = strategyMap;
    }

    public void executeStrategy(String type) {
        Strategy strategy = strategyMap.get(type);
        if (strategy != null) {
            strategy.execute();
        } else {
            throw new IllegalArgumentException("No such strategy: " + type);
        }
    }
}